package com.shuang.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shuang.solr.SolrClientService;

@Controller
@RequestMapping("/solr")
public class SolrHanlder {
	
	@Resource(name="solrClientService")
	private SolrClientService solrClientService;
	
	@RequestMapping("/importGoodsData")
	@ResponseBody
	public Map<String ,Object> importGoodsData(){
		return solrClientService.importAllGoods();
	}
	
	
	@RequestMapping(value="/search" ,method=RequestMethod.GET)
	@ResponseBody
	public Map<String ,Object> searchGoods(String search,
			@RequestParam(value="page" ,defaultValue="1")Integer page,
			@RequestParam(value="rows" ,defaultValue="12")Integer rows) throws UnsupportedEncodingException{
		if(search==""||search==null){
			
			Map<String ,Object> map=new HashMap<String, Object>();
			map.put("status", 400);
			map.put("data", "参数不完整");
			return map;
		}
		search=new String(search.getBytes("iso8859-1"),"utf-8");
		Map<String ,Object> result =solrClientService.searchGoods(page,rows,search);
		
		return result;
	}
	
}
